﻿namespace VLCMusic.Tools;

/// <summary>
/// http操作类
/// </summary>
public class HttpUtil
{
    public static async Task<byte[]> HttpGetByteAsync(string url, Dictionary<string, string> headers = null, int timeOut = 30)
    {
        using var client = new HttpClient();
        client.Timeout = new TimeSpan(0, 0, timeOut);
        if (headers != null)
        {
            foreach (var header in headers)
            {
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
        }
        return await client.GetByteArrayAsync(url);
    }
    public static async Task<string> HttpPostAsync(string url, string postData = "", Dictionary<string, string> headers = null, string contentType = "application/json", int timeOut = 30)
    {
        using var client = new HttpClient();
        client.Timeout = new TimeSpan(0, 0, timeOut);
        if (headers != null)
        {
            foreach (var header in headers)
            {
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
        }
        using var httpContent = new StringContent(postData, Encoding.UTF8);
        if (contentType != null)
        {
            httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
        }
        var response = await client.PostAsync(url, httpContent);
        return await response.Content.ReadAsStringAsync();
    }
}
